<template>
  <div>
     <el-card>
         <div class="alert_box">
      <el-alert
        show-icon
        title="友情提示: 商品的分类只支持三级分类"
        type="warning"
      >
      </el-alert>
    </div>
    <div class="search_box">
      <el-form :model="searchForm" :inline="true">
        <el-form-item>
          <el-cascader
            :options="options"
            placeholder="请选择分类查询"
            :change-on-select="true"
            @change="selectChange"
            :props="searchSelectProps"
            clearable
            v-model="result"
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="请输入物资名称查询"
            v-model="searchForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="回收站" :value="1"></el-option>
            <el-option label="待审核" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="search"
            >查找</el-button
          >
          <el-button
            size="small"
            icon="el-icon-refresh-right"
            type="warning"
            @click="resetForm"
            >重置</el-button
          >
          <el-button
            size="small"
            type="success"
            icon="el-icon-circle-plus-outline"
            @click="handleAdd"
            v-permission="'product:add'"
            >添加
          </el-button>
          <el-button size="small" icon="el-icon-refresh" @click="search"
            >刷新</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="table-box">
      <el-table :data="productList" border stripe>
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="imageUrl" label="图片">
          <template scope="scope">
            <el-image :src="scope.row.imageUrl">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="物资名称"></el-table-column>
        <el-table-column
          prop="pnum"
          show-overflow-tooltip
          label="物资编号"
        ></el-table-column>
        <el-table-column prop="model" label="物资规格">
          <template scope="scope">
            <el-tag>{{ scope.row.model }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="unit" label="单位"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template scope="scope">
            <el-tag v-if="scope.row.status === 0">正常</el-tag>
            <el-tag type="danger" v-if="scope.row.status === 1">回收</el-tag>
            <el-tag type="success" v-if="scope.row.status === 2">待审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="id" label="操作">
          <template scope="scope">
            <span v-if="scope.row.status===0">
              <el-popconfirm  @confirm='handleRemovel(scope.row.id)' title="这是一段内容确定放入回收站吗？">
                <el-button   v-permission="'product:remove'" slot="reference" type="text">回收站</el-button>
              </el-popconfirm>
            </span>
             <span v-if="scope.row.status===1">
              <el-popconfirm @confirm='handleBack(scope.row.id)' title="这是一段内容确定恢复吗？">
                <el-button slot="reference" v-permission="'product:back'" type="text">恢复</el-button>
              </el-popconfirm>
            </span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pageTools
      :total="total"
      :paginationPage="searchForm.pageNum"
      :paginationPagesize="searchForm.pageSize"
      @pageChange="pageChange"
      @pageSizeChange="pageSizeChange"
    ></pageTools>
    <el-dialog
      :title="isEdit ? '编辑物资' : '添加物资'"
      :visible.sync="dialogVisible"
    >
      <el-form :rules="addRole" :model="addform" label-width="100px">
        <el-form-item label="物资名称" prop="name">
          <el-input v-model="addform.name"></el-input>
        </el-form-item>
        <el-form-item label="物资图片">
          <el-upload
            action="http://www.localhost:8989/system/upload/image"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            accept="image/*"
            :on-change="handleChange"
            :on-success="handleSuccess"
            :limit="1"
            :headers="headerObj"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物资规格" prop="model">
              <el-input v-model="addform.model"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类" prop="categoryKeys">
              <el-cascader
                :options="options"
                @change="selectChange"
                :props="addSelectProps"
                clearable
                v-model="addform.categoryKeys"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="addform.unit"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number
                v-model="addform.sort"
                :min="1"
                :max="10"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-form-item label="备注信息" prop="remark">
            <el-input v-model="addform.remark" type="textarea"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button disabled type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
     </el-card>
  </div>
</template>

<script>
import { list } from '../../../api/categories'
import { list as lists, back, remove } from '../../../api/list'
export default {
  data () {
    return {
      dialogVisible: false,
      searchSelectProps: {
        expandTrigger: 'hover',
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }, // 级联搜索选择器配置项
      addSelectProps: {
        expandTrigger: 'hover',
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: false
      }, // 级联搜索选择器配置项
      options: [],
      searchForm: {
        status: 0,
        name: '',
        categorys: '',
        pageSize: 5,
        pageNum: 1,
        unit: ''
      },
      result: [],
      productList: [],
      total: 0,
      isEdit: false,
      addform: {
        model: '',
        sort: '',
        remark: '',
        unit: '',
        categoryKeys: [],
        imageUrl: ''
      },
      addRole: {
        name: [
          { required: true, message: '请输入物资名称', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          }
        ],
        unit: [
          { required: true, message: '请输入物资单位', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '长度在 1 到 10 个字符',
            trigger: 'blur'
          }
        ],
        model: [
          { required: true, message: '请输入物资规格', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          }
        ],
        remark: [
          { required: true, message: '请输入物资说明备注', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          }
        ],
        categorys: [
          { required: true, message: '请输入物资分类', trigger: 'blur' }
        ],
        sort: [{ required: true, message: '请输入地址信息', trigger: 'blur' }],
        categoryKeys: [
          { required: true, message: '请选择物资分类', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async  handleRemovel (id) {
      const res = await remove(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.$notify.success('移入成功')
      this.getproductList()
    },
    async handleBack (id) {
      const res = await back(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.$notify.success('移出成功')
      this.getproductList()
    },
    handlePictureCardPreview () {},
    handleSuccess (response, file, fileList) {
      console.log(response)
    },
    handleRemove () {},
    handleChange () {},
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    },
    pageChange (val) {
      this.searchForm.pageNum = val
      this.getproductList()
    },
    pageSizeChange (val) {
      this.searchForm.pageSize = val
      this.getproductList()
    },
    selectChange (val) {
      this.searchForm.categorys = val.join(',')
    },
    async getParentOptions () {
      const res = await list()
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.options = res.data.rows
    },
    resetForm () {
      this.searchForm = {
        pageSize: 5,
        pageNum: 1,
        status: 0,
        name: '',
        categorys: ''
      }
      this.result = []
      this.getproductList()
    },
    async getproductList () {
      const res = await lists(this.searchForm)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.productList = res.data.rows
      this.total = res.data.total
    },
    search () {
      this.searchForm.pageNum = 1
      this.getproductList()
    }
  },
  created () {
    this.getParentOptions()
    this.getproductList()
  },
  computed: {
    headerObj () {
      const token = localStorage.getItem('token')
      return {
        Authorization: token
      }
    }
  }
}
</script>

<style>
.search_box {
  margin: 20px 0;
}
</style>
